# coding = utf-8
# patches继承于list类,用于处理多个数据,line和polygon都支持

import numpy as np

# from map_command_def import MAP_TYPE_COMMAND_COUNT_DICT


class MapPatches(list):
    def __init__(self):
        self = list([])

    def remove(self, patch):
        if patch in self:
            patch.setDeselected()
            patch.setDefaultAlpha()
            index = super().remove(patch)
            return True, index
        else:
            return False, 0

    def pop(self, index):
        patch = super().pop(index)
        patch.setDeselected()
        patch.setDefaultAlpha
        return patch

    def append(self, patch):
        if self:
            if patch in self:
                return True
            else:
                if patch.maptype != self[0].maptype:
                    print('Not same maptype!')
                    return False
                else:
                    patch.setSelected()
                    patch.setSelectedAlpha()
                    super().append(patch)
                    return True
        else:
            patch.setSelected()
            patch.setSelectedAlpha()
            super().append(patch)
            return True

    def toggleElement(self, patch):
        if self:
            if patch in self:
                return self.remove(patch)
            else:
                return self.append(patch)
        else:
            return self.append(patch)

    def setBelongs(self, belongs_id):
        for patch in self:
            patch.setBelongs(belongs_id)

    def clearBelongs(self):
        for patch in self:
            patch.clearBelongs()

    def setMapType(self, maptype):
        for patch in self:
            patch.setMapType(maptype)

    def setDefaultAlpha(self):
        """
        set alpha to 1
        """
        for patch in self:
            patch.setDefaultAlpha()

    def setSelectedAlpha(self):
        for patch in self:
            patch.setSelectedAlpha()

    def toggleAlpha(self):
        for patch in self:
            if patch.selected:
                patch.setSelectedAlpha()
            else:
                patch.setDefaultAlpha()

    def setObjectType(self, objecttype):
        for patch in self:
            patch.setObjectType(objecttype)

    def clearObjectType(self):
        for patch in self:
            patch.clearObjecttype()

    def clear(self):
        """
        remove all elements
        """
        self.setDefaultAlpha()
        self = list([])

    def get_indexes(self):
        urls = list([])
        for patch in self:
            urls.append(patch.get_index())
        return urls

    def setConfiged(self):
        for patch in self:
            patch.setConfiged()

    def setReconfig(self):
        for patch in self:
            patch.setReconfig()

    def getCenter(self):
        center_list = list([])
        for patch in self:
            if patch.dxftype == 'POINT':
                center_list.append(patch.start)
            else:
                xy_data = patch.get_xy()
                center_list.append(np.average(xy_data, 0))
        return center_list
